var moneyEditApp =moneyEditApp ||{};
moneyEditApp.model = moneyEditApp.model ||{};
moneyEditApp.view = moneyEditApp.view ||{};

$(function(){

    $("#pathUp").text('物业费');
    $("#pathDown").text('物业费详情');

    moneyEditApp.model.moneyModel = Backbone.Model.extend({
        url:window.ctx +"/rest/money/get",
        defaults:{
            money:{
                id:'',
                buildName:'',
                roomNum:'',
                payInTime:'',
                payBeginTime:'',
                payEndTime:'',
                payMoney:'',
                createTime:'',
            },
            owner:{
                buildName:'',
                roomNum:'',
            }

        },
    });
    moneyEditApp.view.executeView = Backbone.View.extend({
        el:"#formDiv",
        model: new moneyEditApp.model.moneyModel(),
        template: _.template($("#moneyTem").html()),
        events: {
            "click #saveBtn": "save",
            "click #backBtn": "back",
        },
        initialize:function(){
            var context = this;
            this.model.fetch({
                data:{
                   /* id:$("form").find("[name=id]").val(),*/
                    ownerId:$("form").find("[name=ownerId]").val(),
                },
                success:function(){
                    context.render();
                },
                error:function(){
                    hintDialog("服务器错误");
                },
            })
        },
        render:function(){
            $(this.el).find("form").append(this.template(this.model.toJSON()));
            $(":text:eq(0)").focus();
        },
        save:function(){
            var context = this;
            var validate = $(context.el).find("form").validate({
                debuge:false,
                rules:{
                    payBeginTime:{
                        required:true,
                    },
                    payEndTime:{
                        required:true,
                    },
                    payMoney:{
                        required:true,
                    },
                },
                buildName: {
                    required: true,
                    remote: {
                        url:window.ctx +"/rest/build/checkBuildIn",
                        type:"post",
                        dataType:"json",
                        data:{
                            buildNum:function(){
                                return $("[name=buildNum]");
                            },
                        },
                    }
                },
                roomNum: {
                    required: true,
                    number: true,
                    remote: {
                        url:window.ctx +"/rest/room/checkRoom",
                        type:"post",
                        dataType:"json",
                        data:{
                            buildNum:function() {
                                return $("[name=buildNum]");
                            },
                            buildNum:function(){
                                return $("[name=buildNum]");
                            },
                        },
                    }
                },
                messages:{
                    payBeginTime:{
                        required:"物业费生效时间不能为空",
                    },
                    payEndTime:{
                        required:"物业费失效时间不能为空",
                    },
                    payMoney:{
                        required:"物业费金额不能为空",
                    },

                },
                submitHandler:function(){
                    var param = {id:$("[name=id]").val()};
                    param["payBeginTime"] = $("[name=payBeginTime]").val();
                    param["payEndTime"] = $("[name=payEndTime]").val();
                    param['ownerId'] = $("[name=ownerId]").val();
                    param["buildName"] = $("[name=buildName]").val();
                    param["roomNum"]= $("[name=roomNum]").val();
                    param["payMoney"]=$("[name=payMoney]").val();
                    $.ajax({
                        url:window.ctx  +"/rest/money/save",
                        type:"post",
                        dataType:"json",
                        contentType:"application/json",
                        data:JSON.stringify(param),
                        success:function(data){
                            if(data){
                                $.dialog.alert("信息增加成功", function () {
                                    location.href = window.ctx + "/owner/list";
                                })
                            }else {
                                console.dir(data);
                                hintDialog("保存申请信息失败！");
                            }
                        },
                        error: function (data) {
                            hintDialog("服务器出错！");
                        }
                    })
                    return;
                }
            });
            $(context.el).find("form").submit();
        },
        back:function(){
            location.href = window.ctx +"/owner/list";
        },
    })
    new moneyEditApp.view.executeView();
})